"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var coupon_service_1 = require("../coupon.service");
var ListComponent = /** @class */ (function () {
    function ListComponent(couponService, _message, localStorageService, confirmServ, router, _elementRef) {
        this.couponService = couponService;
        this._message = _message;
        this.localStorageService = localStorageService;
        this.confirmServ = confirmServ;
        this.router = router;
        this._elementRef = _elementRef;
        this.cardList = []; // 卡券列表
        this.selectedIndex = 0;
        this.typeIndex = 0; // 新人礼券 0  支付礼券 1
        this.typeName = '新人礼品券';
        this.cardModalVisible = false;
        this.receiveCardInfo = { UUID: '', cardListByTag: [], allCardList: [], checkedCards: [] }; // 领取卡券信息
        this.checkedTableCards = []; // 还未确定的选中的卡券
        this.status = null; // 卡券状态  0: 正常 1: 自己关闭 2: 管理员关闭
        this.page = 1;
        this.pageSize = 10;
        this.loading = false;
        /* 添加卡券弹窗参数 */
        this.allChecked = false;
        this.indeterminate = false;
        this.addTag_page = 1;
        this.addTag_pageSize = 7;
        this.addTag_loading = false;
        this.flag = true;
        //二维码
        this.qrcodeVisible = false;
        this.qrcodeShowStatus = false;
        this.canvasWidth = 750;
        this.canvasHeight = 255;
    }
    ListComponent.prototype.ngOnInit = function () {
        this.judgeCouponStatus();
        // 2. Canvas 元素设置一张背景图片
        // 延迟几秒等图片加载完成了再创建画布
        // setTimeout(() => {
        //     this.ctx = this.canvas.getContext('2d');
        //     var img = new Image();
        //     img.src = '/assets/images/discount_card.png';
        //     this.ctx.drawImage(img, 0, 0, this.canvasWidth, this.canvasHeight);
        // }, 500);
    };
    ListComponent.prototype.ngOnChanges = function (changes) {
        if (changes['circleCanvasObjs']) {
            if (this.circleCanvasObjs && this.circleCanvasObjs.length) {
                if (this.canvasHeight) {
                    var node = this._elementRef.nativeElement.querySelector('#qrcode');
                    var width = this.canvasWidth;
                    var height = this.canvasHeight;
                    var baseWidth = 391;
                    var baseHeight = 176;
                    var heightRatio = height / baseHeight;
                    var widthRatio = width / baseWidth;
                    this.draw_rank(width, height, heightRatio, widthRatio);
                }
            }
        }
    };
    ListComponent.prototype.ngAfterViewInit = function () {
        var node = this._elementRef.nativeElement.querySelector('#qrcode');
        var baseWidth = 391;
        var baseHeight = 176;
        // this.canvasWidth = node.width();
        // this.canvasHeight = node.height();
        var heightRatio = this.canvasHeight / baseHeight;
        var widthRatio = this.canvasWidth / baseWidth;
        // console.log('width',this.canvasWidth);
        // console.log('height',this.canvasHeight);
        // this.windowResizeSubscription = Observable.fromEvent($(window), "resize")
        //     .debounceTime(250)
        //     .subscribe(() => {
        //         let width = node.width();
        //         let height = node.height();
        //
        //         let heightRatio: number = height / baseHeight;
        //         let widthRatio: number = width / baseWidth;
        //         this.draw_rank(width,height,heightRatio,widthRatio);
        //     });
        this.draw_rank(this.canvasWidth, this.canvasHeight, heightRatio, widthRatio);
    };
    //
    ListComponent.prototype.draw_rank = function (width, height, heightRatio, widthRatio) {
        if (this.circleCanvasObjs && this.circleCanvasObjs.length) {
            for (var i = 0; i < this.circleCanvasObjs.length; i++) {
                var node = this.circleCanvasObjs[i];
                if (i == 0) {
                    this._draw_rank(this.canvas_rank1, node.title, node.content, node.x, node.y, node.radius, node.fillColor, node.fontSize, width, height, heightRatio, widthRatio);
                }
                if (i == 1) {
                    this._draw_rank(this.canvas_rank2, node.title, node.content, node.x, node.y, node.radius, node.fillColor, node.fontSize, width, height, heightRatio, widthRatio);
                }
                if (i == 2) {
                    this._draw_rank(this.canvas_rank3, node.title, node.content, node.x, node.y, node.radius, node.fillColor, node.fontSize, width, height, heightRatio, widthRatio);
                }
            }
        }
        // this.ctx = this.canvas.getContext('2d');
        // var img = new Image();
        // img.src = '/assets/images/discount_card.png';
        // this.ctx.drawImage(img, 0, 0, this.canvasWidth, this.canvasHeight);
        // this.canvas = this._elementRef.nativeElement.querySelector('#qrcode');
        // this.ctx = this.canvas.getContext('2d');
        // this.ctx.moveTo(100,100);
        // this.ctx.lineTo(700,700);
        // this.ctx.stroke();
        // console.log(this.ctx);
    };
    //绘制排名第一的圆
    ListComponent.prototype._draw_rank = function (canvas, title, content, x, y, radius, fillColor, fontSize, width, height, heightRatio, widthRatio) {
        //获取上下文
        var canvasEl = canvas.nativeElement;
        if (height && width) {
            canvasEl.width = width;
            canvasEl.height = height;
            x = x * widthRatio;
            y = y * heightRatio;
            if (widthRatio < heightRatio) {
                radius = radius * widthRatio;
            }
            else {
                radius = radius * heightRatio;
            }
        }
        var ctx = canvasEl.getContext('2d');
        ctx.beginPath();
        var grd = ctx.createLinearGradient(x, y - radius, x, y + radius);
        grd.addColorStop(0, fillColor);
        grd.addColorStop(1, "white");
        // 填充渐变
        ctx.fillStyle = grd;
        var circle = {
            x: x,
            y: y,
            r: radius //半径
        };
        ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, true);
        ctx.fill();
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.font = "200 " + fontSize + "px Arial";
        ctx.fillStyle = "#FFFFFF"; // text color
        ctx.fillText(title, x, y - 10);
        var subFontSize = fontSize - 2;
        ctx.font = "200 " + subFontSize + "px Arial";
        ctx.fillText(content, x, y + 5);
    };
    ListComponent.prototype.handleOk = function () {
        this.qrcodeVisible = false;
    };
    ListComponent.prototype.handleCancel = function () {
        this.qrcodeVisible = false;
    };
    //生成二维码modal
    ListComponent.prototype.showQrcodeModal = function () {
        this.qrcodeVisible = true;
        // const canvas = this._elementRef.nativeElement.querySelector('#qrcode');
        // const ctx = canvas.getContext('2d');
        //
        // ctx.font="20px Georgia";
        // ctx.fillText("Hello World!",10,50);
        //
        // ctx.font="30px Verdana";
        // var gradient=ctx.createLinearGradient(0,0,canvas.width,0);
        // gradient.addColorStop("0","magenta");
        // gradient.addColorStop("0.5","blue");
        // gradient.addColorStop("1.0","red");
        // ctx.fillStyle=gradient;
        // ctx.fillText("w3school.com.cn",10,90);
        // this.ctx = this.canvas.getContext('2d');
        // var img = new Image();
        // img.src = '/assets/images/discount_card.png';
        // this.ctx.drawImage(img, 0, 0, this.canvasWidth, this.canvasHeight);
    };
    // 判断商户的卡券开通状态
    ListComponent.prototype.judgeCouponStatus = function () {
        var _this = this;
        this.couponService.getAppletInfo().subscribe(function (res) {
            _this.localStorageService.setNowApplet(res.data);
            var applet = _this.localStorageService.getNowApplet();
            _this.status = null;
            var couponExist = false;
            if (!applet.applet_extend.length) {
                _this.status = 1;
            }
            else {
                applet.applet_extend.every(function (val, idx, array) {
                    if (val.type === 1) {
                        couponExist = true;
                        if (val.status === 1) {
                            _this.status = 0;
                            _this.getListDate();
                            _this.getGettingList();
                        }
                        else {
                            _this.status = 1;
                        }
                        return false;
                    }
                    if (!couponExist) {
                        _this.status = 1;
                    }
                    return true;
                });
            }
        });
    };
    ListComponent.prototype.downloadQrcode = function () {
    };
    ListComponent.prototype.showShadow = function () {
        this.qrcodeShowStatus = true;
    };
    ListComponent.prototype.hideShadow = function () {
        this.qrcodeShowStatus = false;
    };
    // 获取所有卡券列表
    ListComponent.prototype.getListDate = function () {
        var _this = this;
        this.loading = true;
        this.couponService.getCouponList(this.page, this.pageSize, this.coupon_name).subscribe(function (res) {
            _this.loading = false;
            _this.total = res.data.total;
            _this.cardList = res.data.data;
            if (!!res.data.data.length) {
                _this.dealDate(res.data.data);
            }
        });
    };
    // 处理数据，获取剩余库存量
    ListComponent.prototype.dealDate = function (date) {
        date.every(function (val, idx, array) {
            if (val.in_stock_type === 1 && !!val.card_coupons_used_report) {
                val.remain = val.in_stock - val.card_coupons_used_report.used_total;
            }
            else {
                val.remain = '不限';
            }
            return true;
        });
    };
    ListComponent.prototype.goAddPage = function () {
        if (this.flag) {
            this.router.navigate(['member/coupon/add']);
            this.flag = false;
        }
    };
    ListComponent.prototype.goDetail = function (id) {
        if (this.flag) {
            this.router.navigate(['member/coupon/detail/' + id]);
            this.flag = false;
        }
    };
    ListComponent.prototype.goEdit = function (id) {
        if (this.flag) {
            this.router.navigate(['member/coupon/edit/' + id]);
            this.flag = false;
        }
    };
    // 搜索
    ListComponent.prototype.search = function () {
        this.page = 1;
        this.getListDate();
    };
    // 删除卡券
    ListComponent.prototype.delCard = function (id) {
        var _this = this;
        this.confirmServ.confirm({
            title: '您是否确认要删除此卡券',
            content: '',
            onOk: function () {
                _this.couponService.delCoupon(id).subscribe(function (res) {
                    if (res.code === 2000) {
                        _this._message.create('success', "\u5361\u5238\u5220\u9664\u6210\u529F");
                        _this.page = 1;
                        _this.getListDate();
                    }
                    else {
                        _this._message.create('error', res.message);
                    }
                });
            },
            onCancel: function () {
            }
        });
    };
    /* 卡券领取设置 */
    // 获取领取设置中的列表
    ListComponent.prototype.getGettingList = function () {
        var _this = this;
        this.couponService.getTagList(this.typeName).subscribe(function (res) {
            _this.receiveCardInfo.UUID = res.data[0].card_coupons_tag_uuid;
            if (!_this.receiveCardInfo.UUID) {
                _this._message.create('error', '卡券未包含此Tag,请联系管理员进行添加');
                return;
            }
            return _this.couponService.getCouponListByTag(_this.receiveCardInfo.UUID).subscribe(function (rs) {
                if (rs.code === 2000) {
                    _this.receiveCardInfo.cardListByTag = rs.data.data;
                    _this.receiveCardInfo.checkedCards = [];
                    if (!!rs.data.data.length) {
                        _this.dealDate(rs.data.data);
                        rs.data.data.every(function (val, idx, array) {
                            _this.receiveCardInfo.checkedCards.push(val.card_coupons_uuid);
                            return true;
                        });
                    }
                }
                else {
                    _this._message.create('error', rs.message);
                }
            });
        });
    };
    // 删除卡券Tag
    ListComponent.prototype.delCardTag = function (card_coupons_uuid) {
        var _this = this;
        this.confirmServ.confirm({
            title: '您是否确认要删除此卡券',
            content: '',
            onOk: function () {
                _this.couponService.delCardTag(card_coupons_uuid, _this.receiveCardInfo.UUID).subscribe(function (res) {
                    if (res.code === 2000) {
                        _this._message.create('success', "\u5361\u5238\u5220\u9664\u6210\u529F");
                        if (_this.typeIndex === 0) {
                            _this.getGettingList();
                        }
                        else {
                            _this.getGettingList();
                        }
                    }
                    else {
                        _this._message.create('error', res.message);
                    }
                });
            },
            onCancel: function () {
            }
        });
    };
    ListComponent.prototype.pageChange = function () {
        this.getListDate();
    };
    // 选中领取卡券列表
    ListComponent.prototype.selectTab = function () {
        if (this.typeIndex === 0) {
            this.typeName = '新人礼品券';
            this.getGettingList();
        }
        else {
            this.typeName = '支付完成卡券';
            this.getGettingList();
        }
    };
    // 添加卡券，绑定tag
    ListComponent.prototype.addBindCoupon = function () {
        this.addTag_page = 1;
        this.checkedTableCards = JSON.parse(JSON.stringify(this.receiveCardInfo.checkedCards));
        this.cardModalVisible = true;
        this.getCradListForTag();
    };
    // 点击添加卡券，获取所有卡券列表
    ListComponent.prototype.getCradListForTag = function () {
        var _this = this;
        this.addTag_loading = true;
        this.couponService.getCouponList(this.addTag_page, this.addTag_pageSize, '').subscribe(function (res) {
            _this.addTag_loading = false;
            _this.addTag_total = res.data.total;
            _this.receiveCardInfo.allCardList = res.data.data;
            if (!!res.data.data.length) {
                _this.dealDate(res.data.data);
                res.data.data.every(function (val, idx, array) {
                    val.checked = false;
                    if (!!_this.checkedTableCards.length) {
                        _this.checkedTableCards.every(function (val1, idx1, array1) {
                            if (val.card_coupons_uuid === val1) {
                                val.checked = true;
                                return false;
                            }
                            return true;
                        });
                    }
                    return true;
                });
            }
            _this.refreshStatus();
        });
    };
    ListComponent.prototype.checkedSingle = function (event, data) {
        var _this = this;
        data.checked = event;
        if (event) {
            this.checkedTableCards.push(data.card_coupons_uuid);
        }
        else {
            this.checkedTableCards.every(function (val, idx, array) {
                if (data.card_coupons_uuid === val) {
                    _this.checkedTableCards.splice(idx, 1);
                    return false;
                }
                return true;
            });
        }
        this.refreshStatus();
    };
    ListComponent.prototype.checkAll = function (value) {
        var _this = this;
        if (value) {
            this.receiveCardInfo.allCardList.every(function (val, idx, array) {
                if (!val.checked) {
                    val.checked = true;
                    _this.checkedTableCards.push(val.card_coupons_uuid);
                }
                return true;
            });
        }
        else {
            this.receiveCardInfo.allCardList.every(function (val, idx, array) {
                if (val.checked) {
                    val.checked = false;
                    _this.checkedTableCards.every(function (val1, idx1, array1) {
                        if (val.card_coupons_uuid === val1) {
                            _this.checkedTableCards.splice(idx1, 1);
                            return false;
                        }
                        return true;
                    });
                }
                return true;
            });
        }
        this.refreshStatus();
    };
    ListComponent.prototype.refreshStatus = function () {
        var allChecked = this.receiveCardInfo.allCardList.every(function (value) { return value.checked === true; });
        var allUnChecked = this.receiveCardInfo.allCardList.every(function (value) { return !value.checked; });
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    };
    // 批量绑定卡券
    ListComponent.prototype.bindingCardByTag = function () {
        var _this = this;
        if (this.checkedTableCards.length > 5) {
            this._message.create('info', "\u6700\u591A\u53EA\u80FD\u9009\u62E9\u4E94\u5F20\u5361\u5238");
            return;
        }
        this.couponService.batchCardBinding(this.receiveCardInfo.UUID, this.checkedTableCards).subscribe(function (res) {
            if (res.code === 2000) {
                _this._message.create('success', "\u5361\u5238\u7ED1\u5B9A\u6210\u529F");
                _this.cardModalVisible = false;
                _this.getGettingList();
            }
            else {
                _this._message.create('error', res.message);
            }
        });
    };
    // 申请开通卡券
    ListComponent.prototype.applyCoupon = function () {
        var _this = this;
        var applet = this.localStorageService.getNowApplet();
        this.couponService.applyCoupon(applet.nick_name, applet.applet_uuid).subscribe(function (res) {
            if (res.code === 2000) {
                _this.judgeCouponStatus();
            }
            else {
                _this._message.create('error', res.message);
            }
        });
    };
    __decorate([
        core_1.Input()
    ], ListComponent.prototype, "circleCanvasObjs", void 0);
    __decorate([
        core_1.ViewChild('canvas_rank1')
    ], ListComponent.prototype, "canvas_rank1", void 0);
    __decorate([
        core_1.ViewChild('canvas_rank2')
    ], ListComponent.prototype, "canvas_rank2", void 0);
    __decorate([
        core_1.ViewChild('canvas_rank3')
    ], ListComponent.prototype, "canvas_rank3", void 0);
    ListComponent = __decorate([
        core_1.Component({
            selector: 'app-list',
            templateUrl: './list.component.html',
            styleUrls: ['./list.component.scss'],
            providers: [coupon_service_1.CouponService]
        })
    ], ListComponent);
    return ListComponent;
}());
exports.ListComponent = ListComponent;
